package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 会员SERVICE实现类
 */
@Service
@Slf4j
public class MemberServiceImpl implements MemberService {


    @Autowired
    private MemberDao memberDao;

    @Override
    public Member getUser(String phone) {
        Member member = memberDao.selectOneUser(phone);
        return member;
    }

    /**
     * 手机端登录保存用户功能
     * @param member
     */
    @Override
    public void saveUser(Member member) {
        memberDao.insertUser(member.getPhoneNumber());
    }

    @Override
    public List<Integer> findMemberCountByMonths(List<String> months) {
        //做一下日期的格式处理
//做一下日期的格式处理
        List<Integer>memberCount = new ArrayList<>();

        try {
            for (String month : months) {
                //获取当前日期并转换为date类型
                Date date1= new SimpleDateFormat("yyyy,MM").parse(month);
                Calendar calendar = Calendar.getInstance();
                //把获取到的每个月传入
                calendar.setTime(date1);
                //获取每个月最大的日期数
                int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                //让得到的月份日期都在每个月的每个月的最后一天
                String date = month+"."+maxDay;
                //用count接收某一天之前的人数
                Integer count = memberDao.findMemberCountBeforeDate(date);
                //把count压入memberCount
                memberCount.add(count);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return memberCount;

    }

}
